import React, { PureComponent,Fragment } from "react";
import { connect } from "dva";
import router from 'umi/router';
import moment from 'moment';
import { Row, Col, Card, Icon, Button, message, Divider, Form, Input, Popconfirm, Badge, Table} from "antd";
import Authorized from '../../../utils/Authorized';
import styles from '../../Pbulic.less';
const FormItem = Form.Item;

const getValue = obj =>
  Object.keys(obj)
    .map(key => obj[key])
    .join(',');




@connect(({ appversion,loading }) => ({
  resdata: appversion.resdata,
  loading: appversion.loading,
  // loading: loading.models.equipment_integrated,
}))
@Form.create()
export default class ListTable extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      currentPage: 1,
      pageSize: 10,
      formValues: {},
      selectedRows:[],
    };
  }

  componentWillUnmount() {

  }


  componentDidMount() {
    const { pageSize } = this.state;
    const { location, dispatch} = this.props;
    const pagesquery = location.query;

    // 列表
    this.getDataList(pagesquery,pageSize,1);


  }


  getDataList(params,pageSize,currentPage) {
    const { dispatch, match } = this.props;
    dispatch({
      type: 'appversion/fetch_appversion_list',
      payload: {
        pageSize,
        currentPage,
        // ...pagesquery,
        ...params,
      },
    });
  }

  // 批量删除
  handleListDel = (id) => {
    const { dispatch } = this.props;
    const { selectedRows,formValues, pageSize, currentPage } = this.state;
    // if(!id && !selectedRows) return;
    // const appVersionIds= id ? [id] : selectedRows;

    id && dispatch({
      type: 'appversion/action_appversion_del',
      payload: {
        ids: [id],
      },
    }).then(()=>{
      message.success("所选版本已删除")
    }).then(()=>{
      this.getDataList(formValues,pageSize,currentPage);
    });


  };

  // 链接跳转-------------------
  handleLink = (action,detailInfoId) =>{
    const { dispatch, match } = this.props;
    // const { formValues, currentPage} = this.state;
    let linkpath;

    switch (action) {
      case 'add':
        linkpath = `/system/appversion/detail/add`;
        break;
      case 'edit':
        linkpath = `/system/appversion/${detailInfoId}/edit`;
        break;
      default:
        break;
    }
    router.push({ pathname:`${linkpath}` });

  }

  // 表单搜索
  handleSearchSubmit = e => {
    e.preventDefault();

    const { form } = this.props;
    const { pageSize } = this.state;

    form.validateFields((err, fieldsValue) => {
      if (err) return;

      this.setState({
        formValues: fieldsValue,
      });

      this.getDataList(fieldsValue,pageSize,1);
    });
  };


  // 重置表单
  handleFormReset = () => {
    const { form } = this.props;
    const {pageSize,currentPage} = this.state;
    form.resetFields();
    this.setState({
      formValues: {},
    });
    this.getDataList({},pageSize,1);
  };


  // 搜索内容
  renderSearchForm() {
    const { form,  pagesquery} = this.props;
    const { getFieldDecorator } = form;
    return (
      <Form onSubmit={this.handleSearchSubmit} >
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>

          <Col md={8} sm={24}>
            <FormItem label="版本号">
              {getFieldDecorator('versionNum',{
                initialValue: pagesquery && pagesquery.appversion ? pagesquery.appversion : undefined,
              })(<Input placeholder="请输入版本号" />)}
            </FormItem>
          </Col>


          <Col md={8} sm={24}>
            <div style={{ float: 'right', marginBottom: 24 }}>
              <Button type="primary" htmlType="submit">
                查询
              </Button>
              <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
                重置
              </Button>
            </div>
          </Col>

        </Row>
      </Form>
    );
  }



  render() {

    const { resdata, loading } = this.props;
    const { selectedRows } = this.state;


    const columns = [
      {
        title: "版本号",
        dataIndex: "versionNum",
      },
      {
        title: "版本描述",
        dataIndex: "versionDesc",
        render: text => text ? text : '--'
      },
      {
        title: "发布日期",
        dataIndex: "uploadTime",
        render: text => text ? moment(text).format("YYYY-MM-DD HH:mm") : '--'
      },
      {
        title: "操作",
        dataIndex: "Action",
        width: "15%",
        render: (text, row) => (
            <span>
              <Authorized authority={'10343'} >
                <a onClick={() => this.handleLink('edit',row.id)}>修改</a>
                <Divider type="vertical" />
              </Authorized>
              <Authorized authority={'10344'} >
                <Popconfirm title="是否确定要删除?" onConfirm={() => this.handleListDel(row.id)}  okText="是" cancelText="否">
                  <a >删除</a>
                </Popconfirm>
              </Authorized>
            </span>
        )
      }
    ];


    return (
      <Card bordered={false}>
          <div className={`${styles.tableList}`}>
            {/*<div className={styles.tableListForm}>{this.renderSearchForm()}</div>*/}

            <div className={styles.tableListOperator}>
              <Authorized authority={'10342'} >
                <Button icon="plus" type="primary"  onClick={() => this.handleLink('add',null)}>
                  新增版本
                </Button>
              </Authorized>

            </div>

            <Table
              loading={loading.reslist}
              rowKey="id"
              dataSource={resdata.list}
              columns={columns}
              onChange={this.handleStandardTableChange}
              pagination={resdata.pagination}
            />

          </div>
      </Card>

    );
  }
}
